import { defineStore } from 'pinia';
import { computed } from 'vue';
import {
  mockSheets,
  currentUser,
  onlineFriends,
} from "../../mock/data";
import { getFileFolderList, getFileFolderNum } from '../../api/fileFolder/index'
const useDashboardStore = defineStore('dashboard', {
  state: () => ({
    // 展示文件类型
    activeFolderId: 'all',
    // 表格数据
    sheets: [],
    // 左侧默认菜单
    defaultMenuItems: [
      {
        id: "all",
        label: "全部文件",
        icon: "Files",
        count: 0
      },
      {
        id: "recent",
        label: "最近",
        icon: "Timer",
        count: 0
      },
      {
        id: "starred",
        label: "已收藏",
        icon: "Star",
        count: 0
      },
      {
        id: "shared",
        label: "共享我的",
        icon: "Share",
        count: 0
      },
    ],
    // 当前选中的那个文件
    currentPath: '全部文件',
    // 添加好友弹框开关
    showAddFriend: false,
    // 好友列表
    onlineFriends: onlineFriends,
    // 聊天的消息
    activeChats: [],
    // 列表排列方式
    viewMode: 'grid',
    // 消息个数
    unreadCount: 0,
    // 消息列表开关
    showMessageCenter: false,
    // 个人信息开关
    showProfile: false,
    // 修改头像弹框开关
    showAvatarDialog: false,
    // 个人信息
    currentUser: currentUser,
    queryParams: {
      pageSize: 10,
      pageNum: 1,
      keyword: "",
      type: 'all',
      userId: JSON.parse(localStorage.getItem('user') as string)._id
    }
  }),
  actions: {
    handleFolderClick(folderId: string) {
      this.queryParams.type = folderId
      this.currentPath = this.defaultMenuItems.find((item) => item.id === folderId)?.label || '全部文件'
      this.reset()
      this.getList()
    },
    async getList() {
      let result = await getFileFolderList(this.queryParams)
      result.data.forEach((item: any) => {
        item.author = JSON.parse(item.author)
      })
      this.sheets = result.data
    },
    reset() {
      this.queryParams.keyword = ''
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
    },
    async getNum() {
      let result = await getFileFolderNum(JSON.parse(localStorage.getItem('user') as string)._id)
      this.defaultMenuItems.forEach((item: any) => {
        if (item.id === 'all') {
          item.count = result.data.allNum
        } else if (item.id === 'recent') {
          item.count = result.data.recentNum
        } else if (item.id === 'starred') {
          item.count = result.data.starredNum
          console.log(result.data.starredNum);

        } else if (item.id === 'shared') {
          item.count = result.data.sharedNum
        }
      })
    }
  }
})

export default useDashboardStore;